<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>springcloud之consul | rongshen</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/highlight.css">

  
  <meta name="description" content="springcloud学习笔记，第三章，consul分布式服务发现和配置管理系统。">
<meta property="og:type" content="article">
<meta property="og:title" content="springcloud之consul">
<meta property="og:url" content="http://example.com/2020/09/25/springcloud%E4%B9%8Bconsul/index.html">
<meta property="og:site_name" content="rongshen">
<meta property="og:description" content="springcloud学习笔记，第三章，consul分布式服务发现和配置管理系统。">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="http://cdn.rongshen2020.com/2020925a1.png">
<meta property="og:image" content="http://cdn.rongshen2020.com/2020925a2.png">
<meta property="og:image" content="http://cdn.rongshen2020.com/2020925a3.png">
<meta property="og:image" content="http://cdn.rongshen2020.com/2020925a4.png">
<meta property="og:image" content="http://cdn.rongshen2020.com/2020925a5.png">
<meta property="og:image" content="http://cdn.rongshen2020.com/2020925a6.png">
<meta property="article:published_time" content="2020-09-25T09:54:36.000Z">
<meta property="article:modified_time" content="2021-05-07T06:49:38.121Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="springCloud">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://cdn.rongshen2020.com/2020925a1.png"><meta name="generator" content="Hexo 6.1.0"></head>

<body>
  <div id="wrapper">
    <header id="header">
  <h1 id="title">
    <a href="/">rongshen</a>
  </h1>
  <nav>
    
    
      
      <a class="nav-link" href="/">Home</a>
    
      
        <span class="nav-spacer">×</span>
      
      <a class="nav-link" href="/archives">Archives</a>
    
      
        <span class="nav-spacer">×</span>
      
      <a class="nav-link" target="_blank" rel="noopener" href="https://github.com/Aaron-boom">Github</a>
    
      
        <span class="nav-spacer">×</span>
      
      <a class="nav-link" href="/2020/04/13/About-Me/">About</a>
    
    
  </nav>
</header>

    <div id="content">
      <article id="post-springcloud之consul" class="article article-type-post" itemprop="blogPost" itemscope>
  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h2 class="article-title" itemprop="headline name">
      springcloud之consul
    </h2>
  


        <div class="article-meta">
          <time class="article-date" datetime="2020-09-25T09:54:36.000Z" itemprop="datePublished">2020-09-25</time>

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
      
        <blockquote>
<p>springcloud学习笔记，第三章，consul分布式服务发现和配置管理系统。</p>
</blockquote>
<span id="more"></span>

<ul>
<li>consul的安装和之前一样，放在linux虚拟机上。同样写一个服务提供者和消费者，查看是否注册进consul，再测试下服务调用，结束。</li>
<li>consul版本：consul_1.8.3_linux_amd64.zip</li>
</ul>
<h2 id="consul简介："><a href="#consul简介：" class="headerlink" title="consul简介："></a>consul简介：</h2><ul>
<li><p>之前的博客已经介绍了两种微服务发现与注册的框架，一个是eureka（翻译：int. 我发现了，我找到了），一个是zookeeper（直译为动物园管理员），以及最后的这个consul（翻译：n. 领事；（古罗马的）两执政官之一）。他们三个取名或多或少都有发现与管理的意思，很有趣。</p>
<p>不过在我写这篇博客的时候，无意中在OSCHINA看到了关于consul的这句话：</p>
</li>
</ul>
<p><img src="http://cdn.rongshen2020.com/2020925a1.png"></p>
<ul>
<li>或许有其他的原因，他们也给了个合理的解释。但是咋们还是得继续努力。</li>
</ul>
<h2 id="consul的安装："><a href="#consul的安装：" class="headerlink" title="consul的安装："></a>consul的安装：</h2><ul>
<li><p>因为需要unzip命令，结果yum下载不了。这里踩了很多坑，网卡配置需要严谨。我在虚拟机创建时网卡设置为的是桥接模式。之前由于未和物理主机设置同一网段，导致ping不同外网。但是可以ping通物理主机，物理主机也可以ping通它，好奇怪。</p>
<p><img src="http://cdn.rongshen2020.com/2020925a2.png"></p>
</li>
<li><p>安装consul：</p>
<pre><code class="JAVA">unzip  consul_1.8.3_linux_amd64.zip
    
#解压之后只有一个consul文件，将该文件复制到bin目录下
cp -rp consul /bin/

#执行一下
consul

我就直接去看UI 界面
[root@localhost consul]# consul agent -dev -ui
游览器本地：http://ip地址:8500/

我在虚拟机中为了其他机器能访问 加参数-client
[root@localhost consul]# consul agent -dev -ui -client 0.0.0.0
</code></pre>
</li>
</ul>
<h2 id="服务提供者注册进consul："><a href="#服务提供者注册进consul：" class="headerlink" title="服务提供者注册进consul："></a>服务提供者注册进consul：</h2><ol>
<li><p>建module，<code>cloud-provider-consul-payment8006</code></p>
</li>
<li><p>pom：</p>
<pre><code class="java">&lt;!--consul--&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-consul-discovery&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;!--web开发依赖--&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
&lt;/dependency&gt;
</code></pre>
</li>
<li><p>yml：</p>
<pre><code class="java">server:
  port: 8006

spring:
  application:
    name: consul-provider-payment
  cloud:
    consul:
      host: 192.168.0.11
      port: 8500
      discovery:
        service-name: $&#123;spring.application.name&#125;
</code></pre>
</li>
<li><p>主启动类：</p>
<p>无特别。</p>
</li>
<li><p>controller，随便写一个：</p>
<pre><code class="java">@RestController
@Slf4j
public class PaymentController &#123;
    @Value(&quot;$&#123;server.port&#125;&quot;)
    private String serverPort;

    @RequestMapping(value = &quot;/payment/consul&quot;)
    public String paymentConsul()&#123;
        return &quot;springcloud with consul：&quot;+serverPort+&quot;\t&quot;+ UUID.randomUUID().toString();
    &#125;

&#125;
</code></pre>
</li>
<li><p>启动测试：</p>
<p><img src="http://cdn.rongshen2020.com/2020925a3.png"></p>
</li>
<li><p>这时，consul的ui界面这里出现了一个错误（大致意思我理解为服务检查错误）：</p>
<p><img src="http://cdn.rongshen2020.com/2020925a4.png"></p>
<p>百度解决方案时，说是主机映射的问题。</p>
<p>修改主机映射文件hosts之后，完美解决服务未发现问题。</p>
<p><img src="http://cdn.rongshen2020.com/2020925a5.png"></p>
</li>
</ol>
<h2 id="服务消费者注册进consul："><a href="#服务消费者注册进consul：" class="headerlink" title="服务消费者注册进consul："></a>服务消费者注册进consul：</h2><ol>
<li><p>建model，<code>cloud-consumer-consul-order80</code></p>
</li>
<li><p>pom</p>
</li>
<li><p>yml</p>
</li>
<li><p>主启动类</p>
</li>
<li><p>RestTemplate和之前都是一样的</p>
</li>
<li><p>controller</p>
<pre><code class="java">@RestController
@Slf4j
public class OrderConsulController &#123;
    public static final String INVOKE_URL = &quot;http://consul-provider-payment&quot;;

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = &quot;/consumer/payment/consul&quot;)
    public String paymentInfo()&#123;
        String result = restTemplate.getForObject(INVOKE_URL+&quot;/payment/consul&quot;,String.class);
        return result;
    &#125;
&#125;
</code></pre>
<p><img src="http://cdn.rongshen2020.com/2020925a6.png"></p>
</li>
</ol>

      
    </div>
    
    
    <div class="article-category">
      
        <b>Categories:</b>
        <a class="article-category-link" href="/categories/spring-cloud/">spring cloud</a>
      
      
        <br/>
      
      
        <b>Tags:</b>
        <a class="article-tag-none-link" href="/tags/springCloud/" rel="tag">springCloud</a>
      
    </div>
    
    
  </div>
</article>

  
<nav id="article-nav" class="article-nav">
  
    <a href="/2020/09/25/eureka%E3%80%81zookeeper%E3%80%81consul%E4%B8%8ECAP%E7%90%86%E8%AE%BA/" id="article-nav-newer" class="article-nav-link-wrap newer">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          eureka、zookeeper、consul与CAP理论
        
      </div>
    </a>
  
  
    <a href="/2020/09/24/springcloud%E4%B9%8Bzookeeper/" id="article-nav-older" class="article-nav-link-wrap older">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">
        
          springcloud之zookeeper
        
      </div>
    </a>
  
</nav>






    </div>
  </div>
  




<div id="settings-container">
  <div id="dark-mode">dark</div>
  <div id="sans-font">sans</div>
</div>
<script type="text/javascript">
let d=document,r=d.documentElement.style,f=r.setProperty.bind(r),l=localStorage,s=l.getItem('s')||(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches),n=l.getItem('n'),m=d.getElementById("dark-mode"),b=()=>{f('--bg-color','#fafafa');f('--code-bg-color','#f4f4f4');f('--text-color','#212121');f('--secondary-color','#808080');f('--tertiary-color','#b0b0b0');f('--link-color','#b5c8cf');f('--link-hover-color','#618794');f('--link-bg-color','#dae4e7');f('--selection-color','#dae4e7');m.innerHTML="dark"},c=()=>{f('--bg-color','#212121');f('--code-bg-color','#292929');f('--text-color','#fff');f('--secondary-color','#c0c0c0');f('--tertiary-color','#6e6e6e');f('--link-color','#4d6b75');f('--link-hover-color','#96b1bb');f('--link-bg-color','#5d828e');f('--selection-color','#acc1c9');m.innerHTML="light"},o=d.getElementById("sans-font"),e=()=>{f('--body-stack','"Lora", "Georgia", "Times New Roman", serif');o.innerHTML="sans"},g=()=>{f('--body-stack','"Lato", "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", "Verdana", sans-serif');o.innerHTML="serif"};m.onclick=()=>{if(s==2){s=1;l.setItem('s',s);c()}else{s=2;l.setItem('s',s);b()}};o.onclick=()=>{if(n==2){n=1;l.setItem('n',n);g()}else{n=2;l.setItem('n',n);e()}};if(!s){s=2;l.setItem('s',2)};if(s==1){c()};if(!n){n=2;l.setItem('n',2)};if(n==1){g()};
</script>




</body>
</html>
